package com.dx.mes.service.process.service;

import static com.dx.mes.common.bean.def.GenericDef.EVT_PRD_CANCEL_LGON;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEINCANCEL_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEINCANCEL_PRD_STATUS_INVALIED;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEIN_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.E_READ_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.E_RET_PRD_INFO;
import static com.dx.mes.common.bean.def.GenericDef.PROC_IN_PROCESS;
import static com.dx.mes.common.bean.def.GenericDef.PROC_WAIT;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FPPMVINCCL;
import static com.dx.mes.common.bean.def.GenericDef._NORMAL;
import static com.dx.mes.common.bean.def.GenericDef._SPACE;

import org.hibernate.LockMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Ret_prd_info;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.process.service.IFppmoveinCancelService;
import com.dx.mes.facade.process.tx.FppmoveinCancel.FppmoveinCancelI;
import com.dx.mes.facade.process.tx.FppmoveinCancel.FppmoveinCancelIA;
import com.dx.mes.facade.process.tx.FppmoveinCancel.FppmoveinCancelO;

@Service("fppmoveinCancelService")
public class FppmoveinCancelService implements IFppmoveinCancelService{
	private LogUtils logUtils;
	
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Override
	public String subMainProc(String evt_no, String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FppmoveinCancelService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FppmoveinCancelService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		String outMsg = null;
		FppmoveinCancelI inTrx = new FppmoveinCancelI();
		FppmoveinCancelO outTrx = new FppmoveinCancelO();
		
		outTrx.setTrx_id(T_FPPMVINCCL);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FppmoveinCancelI.class);
			
			char action_flg = inTrx.getAction_flg().charAt(0);
            switch (action_flg) {
    			case 'C'://单片取消入账
	    			moveinCancel(inTrx,outTrx);
	    			break;
                default:
                    outTrx = errActionFnc();
            }
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retPrdInfoDao.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}
	
	private void moveinCancel(FppmoveinCancelI inTrx, FppmoveinCancelO outTrx) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		FppmoveinCancelIA iary = inTrx.getIary().get(0);
    	if(inTrx.getIary().size() == 0){
            outTrx.setRtn_code(E_FPPMOVEIN_INVALID_INPUT + "");
            outTrx.setRtn_mesg("入账信息不正确");
            return;
    	}
    	
    	Ret_prd_info ret_prd_info = retPrdInfoDao.getWithLock(iary.getPrd_seq_id(), LockMode.UPGRADE);
        if (ret_prd_info == null) {
            outTrx.setRtn_code(E_RET_PRD_INFO + E_READ_NOT_FOUND + "");
            outTrx.setRtn_mesg("屏体信息不存在");
            return;
        }
        
		/**
		 * check prd info
		 */
		rtnBeen = checkPrdInfoProc(ret_prd_info);
		if (rtnBeen.getlRc() != _NORMAL) {
			outTrx.setRtn_code(rtnBeen.getlRc() + "");
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return;
		}
		
		/**
		 * update prd && box info
		 */
		if(!Util.isSpaceCheck(iary.getLot_id())&&iary.getLot_id().equals(ret_prd_info.getLot_id())){
			ret_prd_info.setLot_id("");//FQC退片时清空批次lotId
		}
		/**
		 * set prd next information 
		 */
		ret_prd_info.setNx_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
		ret_prd_info.setNx_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
		ret_prd_info.setNx_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
		ret_prd_info.setNx_path_id_fk(ret_prd_info.getCr_path_id_fk());
		ret_prd_info.setNx_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
		ret_prd_info.setNx_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
		ret_prd_info.setNx_tool_run_mode(ret_prd_info.getCr_tool_run_mode());
		ret_prd_info.setNx_toolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
		/**
		 * set prd current information 
		 */
//		ret_prd_info.setTool_id_fk(_SPACE);
		ret_prd_info.setTool_port_id_fk(_SPACE);
		ret_prd_info.setCr_ope_id_fk(ret_prd_info.getPv_ope_id_fk());
		ret_prd_info.setCr_ope_no_fk(ret_prd_info.getPv_ope_no_fk());
		ret_prd_info.setCr_ope_ver_fk(ret_prd_info.getPv_ope_ver_fk());
		ret_prd_info.setCr_path_id_fk(ret_prd_info.getPv_path_id_fk());
		ret_prd_info.setCr_path_ver_fk(ret_prd_info.getPv_path_ver_fk());
		ret_prd_info.setCr_proc_id_fk(ret_prd_info.getPv_proc_id_fk());
		ret_prd_info.setCr_tool_run_mode(ret_prd_info.getPv_tool_run_mode());
		ret_prd_info.setCr_toolg_id_fk(ret_prd_info.getPv_toolg_id_fk());
		ret_prd_info.setCr_tool_id_fk(_SPACE);
		
		/**
		 * set prd pv information
		 */
		ret_prd_info.setPv_prd_stat_fk(ret_prd_info.getPrd_stat());
        ret_prd_info.setPv_ope_id_fk(_SPACE);
        ret_prd_info.setPv_ope_no_fk(_SPACE);
        ret_prd_info.setPv_ope_ver_fk(_SPACE);
        ret_prd_info.setPv_path_id_fk(_SPACE);
        ret_prd_info.setPv_path_ver_fk(_SPACE);
        ret_prd_info.setPv_tool_id_fk(_SPACE);
        ret_prd_info.setPv_toolg_id_fk(_SPACE);
        ret_prd_info.setPv_proc_id_fk(_SPACE);
        ret_prd_info.setPv_tool_run_mode(_SPACE);
		/**
		 * set prd event information
		 */
		ret_prd_info.setSlot_no(ret_prd_info.getPv_slot_no());
		ret_prd_info.setPrd_stat(PROC_WAIT);
		ret_prd_info.setSht_ope_msg(inTrx.getSht_ope_msg());
		ret_prd_info.setEvt_cate(EVT_PRD_CANCEL_LGON);
		ret_prd_info.setEvt_timestamp(Util.getCurrentTimestamp());
		ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
		if("Y".equals(inTrx.getBack_flg())){
			ret_prd_info.setExt_8("S");
		}
		retPrdInfoDao.update(ret_prd_info);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}

	private SimpleRtnBeen checkPrdInfoProc(Ret_prd_info ret_prd_info) {
		/**
		 * 检查玻璃状态，必须为INPR
		 */
		if(!PROC_IN_PROCESS.equals(ret_prd_info.getPrd_stat())){
			return new SimpleRtnBeen(E_FPPMOVEINCANCEL_PRD_STATUS_INVALIED, 
					"屏体状态不正确，必须为["+PROC_IN_PROCESS+"],请确认");
		}
		return new SimpleRtnBeen(_NORMAL);
	}

    public FppmoveinCancelO errActionFnc() {
    	FppmoveinCancelO fppmoveinCancelO = new FppmoveinCancelO();
    	fppmoveinCancelO.setTrx_id(T_FPPMVINCCL);
    	fppmoveinCancelO.setType_id(TRX_OUT);
    	fppmoveinCancelO.setRtn_code(E_FPPMOVEINCANCEL_INVALID_INPUT + "");
    	fppmoveinCancelO.setRtn_mesg("Invalid Action_flg");
        return fppmoveinCancelO;
    }
}
